<?
/* / / / / / ENGINE  ROOM / / / / / / /
|	          MOD CLASS         	  |
|        (aka. block element)         |
|                                     |
| version:     0.0.1                  |
| last edit:   20091128               |
| package:     beta.r.0.0             |
/ / / / / / / / / / / / / / / / / / /*/


class MOD_STANDALONE {
	function MOD($mod, $vars){
		$this->_addvars($vars);
		$this->_ref();
		$this->mod = $mod;
		if(!$this->id) $this->id = $this->wtype."_".$this->mod;
	}
	
	function _addvars($vars){ foreach($vars as $k=>$v) $this->$k=$v; }
	
	function _ref() { 
		$this->ref = json_decode(engine_get("style:".$this->wtype.".default"));
		if($this->nobot) $this->ref->nobot = $this->nobot;
		if($this->tleref) foreach($this->tleref as $k=>$v) $this->ref->title->{$k} = $v;
		if($this->meta) foreach($this->meta as $k=>$v) $this->ref->{$k} = $v;
		if(!$this->class) $this->class = $this->ref->class;
	}
	
	function _title($str){
		global $DB;
		if($this->h) $this->ref->title->h = $this->h; else if(is_array($this->ref->title->h)) $this->ref->title->h = $this->ref->title->h[0];
		if(is_int($str)) $str = $DB->lang($str);
		
		if(!$this->ref->title->plain) {
			$img = new MEDIA($this->ref->title);
			return $img->_txt($str);
		} else return "\n<".$this->ref->title->h.">$str</".$this->ref->title->h.">";
	}
	
	function _link($vars=false){
		global $DB;
		if($vars)$this->_addvars($vars);
		if(isset($this->href) && is_int($this->href))  $this->_addvars($DB->parse("CONCAT('http://',url) AS href, nameid AS alt","db_link","ID=".$this->href));
		else if(isset($this->page)){
			$this->href = $this->page;
			if(!isset($this->descr)) $this->descr = (int)$DB->short("nameid","pages","name='".((strpos($this->page,"&"))?strstr($this->page, '&', true) : $this->page)."'");
		} else if(isset($this->file)){
			$this->href = "./DB/files/".$this->file;
			if(!isset($this->descr)) $this->descr = (int)$DB->short("nameid","pages","name='".((strpos($this->page,"&"))?strstr($this->page, '&', true) : $this->page)."'");
		 }else if(isset($this->imgid)){
		 	$IMG = new MEDIA($this->img);
			$this->descr = $IMG->_img();
		}
		if(!isset($this->target)) $this->target = isset($this->page)? "_self" : "_blank"; 
		if(isset($this->alt) && is_int($this->alt))   $this->alt = $DB->lang($this->alt);
		if(is_int($this->descr)) $this->descr = $DB->lang($this->descr);
		return "<a href='".$this->href."' target='".$this->target."' ".(isset($this->class)?"class='".$this->class."'":'').">".$this->descr."</a>";
	}
	
	function _pregmatch($text){
		global $DB;
		$text = stripslashes($text);
		if(preg_match_all('/(<%)([^%]*)(%>)/',$text,$raw, PREG_SET_ORDER)){
			$replace = $replace_by = array();
			foreach($raw as $v) if(!in_array($v[0],$replace)){
				$replace[]=$v[0];
				$v[2]=json_decode((strlen($v[2])>3)? $v[2]: $DB->short("json","moddump","ID=".$v[2]),true);
				$replace_by[] = mod_get("single",key($v[2]),current($v[2]));
			}
			$text = str_replace($replace, $replace_by, $text);
		}
		return $text;
	}
	
	
	function _parse($body, $class=false, $vars=false){
		global $DB, $_init;
		
		if(!$class) $class = isset($this->class)? $this->class : engine_get('style:'.$this->wtype.'.default');
		if($vars) $this->_addvars($vars);
		
		$win.= ($this->nameid && $this->ref->title->position=="first")? $this->_title((int)$this->nameid): (($this->title)?$this->title:"");
		$win.="<div id='".$this->id."' class='".$this->wtype." $class' ".(isset($this->style)?"style='".$this->style."'":"").">\n";
		
		if($this->unshift) $win .= (is_array($this->unshift))?implode("\n",$this->unshift) : $this->unshift;
		
		if($this->nameid && $this->ref->title->position=="in") $win .= $this->_title((int)$this->nameid);
		if(isset($this->textid)) $win .= "\n<p class='tblock'>".$this->_pregmatch($DB->lang($this->textid,"long"))."</p>";
		
		if($body) $win .= "\n".$this->_pregmatch($body);
		
		if($this->push) $win .= (is_array($this->push))?implode("\n",$this->push) : $this->push;
		
		if($class != "blank" && !$this->ref->nobot) $win.=($this->bot)?"<img id='".$this->id."_bot' src='./engine.room/gfx/img.php?id=".$this->bot['id']."&sze=".$this->bot['sze']."' class='bot' alt='".$this->bot['alt']."' style='height:auto' />":"<div id='".$this->id."_bot' class='bot $class'></div>";
		$win .= "</div>";
		
		if($this->post) $win .= (is_array($this->post))?implode("\n",$this->post) : $this->post;
		
		return $win;
	}
	
	function _el($content=false, $attr=false, $type="div") {
		if($attr) foreach($attr as $k=>$a) $at .= "$k='$a' ";
		return "<$type $at".(is_array($content)? $this->_el: (($content)? $content."</".$type.">":(($type=="div")?"></div>":"/>")))."\n";
	}
	
}